package com.lmonkey.filter;

import com.lmonkey.dao.Basedao;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebFilter( "/admin/register")
public class Register implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest reqq = (HttpServletRequest)req;
        HttpServletResponse respp = (HttpServletResponse) resp;
        //设置字符集
        reqq.setCharacterEncoding("UTF-8");
        respp.setContentType("text/html;charset=utf-8");

        String username = reqq.getParameter("username");

        PrintWriter out = respp.getWriter();
        //验证用户名是否为空
        if (username.equals("")){
            out.println("<script>alert('用户名不能为空');location.href='/lyq/shangc/reg.jsp';</script>");
            out.close();
            return;
        }else {
            Pattern pt = Pattern.compile("[a-zA-Z\\u4E00-\\u9FA5]*");
            Matcher mt = pt.matcher(username);
            if (!mt.matches()){
                out.println("<script>alert('用户名格式不正确');location.href='/lyq/shangc/reg.jsp';</script>");
                out.close();
                return;
            }else {
                Connection conn = Basedao.getconn();
                PreparedStatement ps = null;
                ResultSet rs = null;
                int count = 0;
                try {
                    String sql = "select count(*) from lmonkeyshop where user_id = ? ";
                    ps = conn.prepareStatement(sql);
                    ps.setObject(1,username);
                    rs = ps.executeQuery();
                    while(rs.next()){
                        count = rs.getInt(1);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (count>0){
                    out.println("<script>alert('用户名已存在');location.href='/lyq/admin/admin_useradd.jsp';</script>");
                    out.close();
                    return;
                }
            }
        }
        //验证用户姓名是否为空
        String name = reqq.getParameter("name");
        if (name.equals("")){
            out.println("<script>alert('用户姓名不能为空');location.href='/lyq/shangc/reg.jsp';</script>");
            out.close();
            return;
        }else{
            Pattern pt = Pattern.compile("[\\u4e00-\\u9fa5]*");
            Matcher mt = pt.matcher(name);
            if (!mt.matches()){
                out.println("<script>alert('用户姓名格式不正确');location.href='/lyq/shangc/reg.jsp';</script>");
                out.close();
                return;
            }
        }
        //验证密码以及确认密码
        String password = reqq.getParameter("password");
        String pass = reqq.getParameter("pass");
        if (password.equals("")||pass.equals("")){
            out.println("<script>alert('密码或确认密码不能为空');location.href='/lyq/shangc/reg.jsp';</script>");
            out.close();
            return;
        }else if (!password.equals(pass)){
            out.println("<script>alert('两次密码输入不一致');location.href='/lyq/shangc/reg.jsp';</script>");
            out.close();
            return;
        }else{
            Pattern pt = Pattern.compile("^[A-Za-z0-9-]{6,15}$");
            Matcher mt = pt.matcher(password);
            if (!mt.matches()){
                out.println("<script>alert('密码格式有误');location.href='/lyq/shangc/reg.jsp';</script>");
                out.close();
                return;
            }
        }
        //验证邮箱
        String email = reqq.getParameter("email");
        if (!email.equals("")){
            Pattern pt = Pattern.compile("^\\w+@[0-9a-z]+\\.[a-z]+$");
            Matcher mt = pt.matcher(email);
            if (!mt.matches()){
                out.println("<script>alert('邮箱格式不正确');location.href='/lyq/shangc/reg.jsp';</script>");
                out.close();
                return;
            }
        }
        //验证手机号码
        String mobile = reqq.getParameter("mobile");
        if (mobile.equals("")){
            out.println("<script>alert('手机号码不能为空');location.href='/lyq/shangc/reg.jsp';</script>");
            out.close();
            return;
        }else {
            Pattern pt = Pattern.compile("^[1][3,5,7,8]\\d{9}$");
            Matcher mt = pt.matcher(mobile);
            if (!mt.matches()){
                out.println("<script>alert('手机号码格式不正确');location.href='/lyq/shangc/reg.jsp';</script>");
                out.close();
                return;
            }
        }
        //验证收货地址
        String address = reqq.getParameter("address");
        if (address.equals("")){
            out.println("<script>alert('收货地址不能为空');location.href='/lyq/shangc/reg.jsp';</script>");
            out.close();
            return;
        }
        //验证验证码是否正确
        HttpSession session = reqq.getSession();
        String verycode = reqq.getParameter("verycode");
        String syscode = (String)session.getAttribute("code");
        if (verycode.equals("")){
            out.println("<script>alert('验证码不能为空');location.href='/lyq/shangc/reg.jsp';</script>");
            out.close();
            return;
        }else  if (!syscode.equals(verycode)){
            out.println("<script>alert('验证码输入有误');location.href='/lyq/shangc/reg.jsp';</script>");
            out.close();
            return;
        }


        //不通过则直接写一个return;
        chain.doFilter(reqq, respp);//通过则使用这条数据。
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
